import store from '../repo/store/store'
import {authActionsTypes} from '../repo/store/auth'
import UserAPI from '../repo/api/UserAPI'
import {userActionsTypes, userTypes} from '../repo/store/user'

export function loginWithSign(next, to, sign) {

      store.dispatch(authActionsTypes.LOGIN_BY_SIGN, {sign}).then((result) => {
        UserAPI.getUserInfo().then((result) => {
          store.commit(userTypes.SAVE_USER_INFO, result.responseData);
          store.dispatch(userActionsTypes.FETCH_ENTERPRISE_INFO).then(() => {
            store.dispatch('memberInfo/getMemberInfo')
            next(to.path)
          }).catch((e) => {

            next({'name': 'auth-failed'})
          })
        }).catch((e) => {
          next({'name': 'auth-failed'})
        })

  }).catch((err) => {
    console.error('login failed in err', err);
    next({'name': 'auth-failed'})
  })
}
